package com.kjj.org.service.impl;

import com.kjj.basic.util.MD5Utils;
import com.kjj.basic.util.StrUtils;
import com.kjj.org.domain.Employee;
import com.kjj.org.mapper.EmployeeMapper;
import com.kjj.org.service.IEmployeeService;
import com.kjj.basic.service.impl.BaseServiceImpl;
import com.kjj.user.domain.Logininfo;
import com.kjj.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kan
 * @since 2022-09-01
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
//做增删改的时候不仅要操作本表，还要操作logininfo的表
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void add(Employee employee) {
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        salt = "yaosang" + salt + "jfjfjfjfj";
        String securityPwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        logininfo.setSalt(salt);
        logininfo.setPassword(securityPwd);

        logininfo.setType(Logininfo.LOGININFO_TYPE_ADMIN);
        logininfo.setDisable(1);
        logininfoMapper.save(logininfo);


        //要保存登录信息id，还要把密码加密处理
        employee.setLogininfoId(logininfo.getId());
        employee.setSalt(salt);
        employee.setPassword(securityPwd);
        employeeMapper.save(employee);
    }

    @Override
    public void del(Serializable id) {

        Employee employee = employeeMapper.loadById(id);

        Long logininfoId = employee.getLogininfoId();

        logininfoMapper.remove(logininfoId);
        employeeMapper.remove(id);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }
}
